package com.apes.fn.scm.settleCheck.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.scm.contract.FnContract;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：赋能商户结算政策主表
 *
 * @author zhuyh
 * @time 14:40
 * @date 2020/2/28
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PartnerCheck.CONST_TABLE_NAME)
public class PartnerCheck extends Model {

    public static final String CONST_TABLE_NAME = "fn_partner_check";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "结算政策编码", padding = 12)
    private String id;

    @Column(name = "name", length = 250)
    @Comment("结算政策名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_id", referencedColumnName = "id", nullable = true)
    @Comment("赋能客户")
    private PartyRole fnCustomer;  //商户

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = true)
    @Comment("销售部门")
    private Dept saleDept;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", referencedColumnName = "id", nullable = false)
    @Comment("赋能合同")
    private FnContract fnContract;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;


    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    @Comment("政策有效期起")
    private Date startDate; //政策有效期起

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("政策有效期止")
    private Date endDate; //政策有效起止


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "period_type", referencedColumnName = "id")
    @Comment("结算周期")
    private Enumeration periodType; //(单次;每月;每季度;每半年;每年);

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "firstcheck_date")
    @Comment("首次结算日期")
    private Date firstCheckDate; //首次结算日期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "lastcheck_date")
    @Comment("上次结算日期")
    private Date lastCheckDate; //上次结算日期

    @Column(name = "state")
    @Comment("状态(create:创建,delete:删除,approve:审核,repeal:终止,extension:延期)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "extension_uid", referencedColumnName = "id")
    @Comment("延期人")
    private PartyRole extensionUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "extension_date")
    @Comment("延期日期")
    private Date extensionDate;


    @OneToMany(mappedBy = "partnerCheck", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PartnerCheckItem> partnerCheckItems = new HashSet();


    @OneToMany(mappedBy = "partnerCheck", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PartnerCheckClassify> partnerCheckClassifies = new HashSet();


    public Set<PartnerCheckItem> getPartnerCheckItems() {
        for (PartnerCheckItem item : partnerCheckItems) {
            // 设置主表
            item.setPartnerCheck(this);
        }
        return partnerCheckItems;
    }


    public Set<PartnerCheckClassify> getPartnerCheckClassifies() {
        if (partnerCheckClassifies == null) {
            partnerCheckClassifies = new HashSet<>();
        }
        for (PartnerCheckClassify item : partnerCheckClassifies) {
            // 设置主表
            item.setPartnerCheck(this);
        }
        return partnerCheckClassifies;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if (this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }


    public Set<PartnerCheckItem> lastDateUp() {
        for (PartnerCheckItem item : partnerCheckItems) {
            // 设置主表
            item.setLastCheckDate(this.getLastCheckDate());
        }
        return partnerCheckItems;
    }
//    public void setPartnerCheckClassifies()(HashSet<PartnerCheckClassify> partnerCheckClassifies){
//        this.partnerCheckClassifies.clear();
//        if (partnerCheckClassifies != null) {
//            this.partnerCheckClassifies.addAll(partnerCheckClassifies);
//        }
//    }
}
